package login;

import bean.Message;
import javax.persistence.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet(name = "Register",urlPatterns = {"Register"})
public class Register extends HttpServlet {
    private static final EntityManagerFactory entityManagerFactory;
    private static final String PERSISTENCE_UNIT_NAME = "MyJPADemoPU";

    static {
        entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    }

    private EntityManager getEntityManager() {
        return entityManagerFactory.createEntityManager();
    }


    //判断注册信息是否成功，保证两次输入密码一致
    protected static boolean register_success(String name,String email,String password,String qrpassword,String phone)
    {
        if(!password.equals(qrpassword)||(email.length()<=0)||(name.length()<=0)||(phone.length()<=0)||(password.length()<=0))
        {
            return false;
        }
        return true;
    }

    protected void register_check(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {

        String name = req.getParameter("name");
        String email = req.getParameter("email");
        String password = req.getParameter("password");
        String password2 = req.getParameter("password2");
        String telephone = req.getParameter("telephone");
        if (!register_success(name, email, password, password2, telephone)) {
            resp.sendRedirect("error.jsp");
        } else {
            boolean flag=false;
            //判断注册信息是否存在
            Message message = null;
            EntityManager em = getEntityManager();
            EntityTransaction tx = em.getTransaction();
            Query query = em.createQuery("select mes from Message mes where mes.name= :name");
            query.setParameter("name",name);
            List list = query.getResultList();
            if (list.size() != 0) {
                for (Object i : list) {
                    message = (Message) i;
                    if (message.getname().equals(name)) {
                        flag = true;
                        break;
                    }
                }
            }
            //将注册信息写入数据库
            if(!flag)
            {
                try {
                    message = new Message(name, email, password, telephone, "cat.jpg", null);
                    tx.begin();
                    em.persist(message);
                    tx.commit();
                }catch (Exception e)
                {
                    tx.rollback();
                    resp.sendRedirect("error.jsp");
                }finally {
                    em.close();
                }
            }
            System.out.println("注册成功,登录到用户界面");
            req.getSession().setAttribute("LocalUser",message);
            resp.sendRedirect("usercenter.jsp");
        }
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        register_check(req,resp);
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

}
